package canss.webservice.controls;


import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;

import canss.encoder.DBStringEncoder;
import canss.webservice.model.MID;

import weblogic.jdbc.SQLExceptionWrapper;

import java.io.Serializable;

@ControlImplementation
public class MIDControlImpl implements MIDControl, Serializable
{
	private static final long serialVersionUID = 1L;

	@Control
	private BusinessSystemDB businesssystemDB;

	public Boolean createModuleID(String _mid) throws SQLExceptionWrapper
	{
		String tmp = DBStringEncoder.Server2DB(_mid);
		if (tmp == null)
			return false;
		int ret = businesssystemDB.createModuleID(tmp);
		return ret>0;
	} 

	public Boolean isDuplicatedCID(String _mid) throws SQLExceptionWrapper
	{
		MID[] personlist = businesssystemDB.findModuleID(DBStringEncoder
				.Server2DB(_mid));
		if (personlist!=null && personlist.length > 0)
			return true;
		return false;
	}

	public String[] getMIDWithin(int _numbers) throws SQLExceptionWrapper
	{
		MID[] personlist = businesssystemDB.getModulesWithin(_numbers);
		if(personlist==null)
			return null;
		String[] ret = new String[personlist.length];
		for (int i = 0; i < personlist.length; i++)
			ret[i] = DBStringEncoder.DB2Server(personlist[i].getMID());
		return ret;
	}

	public Integer getNumberofMudules() throws SQLExceptionWrapper
	{
		return businesssystemDB.getNumberofModules();
	}
}